package com.itcast.controller;

import com.itcast.pojo.AliyunOSSProperties;
import com.itcast.pojo.Result;
import com.itcast.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Slf4j
public class UploadController {
//    public static final String endpoint = "https://oss-cn-beijing.aliyuncs.com";
//    public static final String bucketName = "web-132du";

//    @Value("${aliyun.oss.endpoint}")
//    private String endpoint;
//    @Value("${aliyun.oss.bucketName}")
//    private String bucketName;

    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("上传文件");
        /*String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf('.'));
        String uuid = UUID.randomUUID().toString();
        String newFileName = uuid + ext;
        file.transferTo(new File("D:/"+newFileName));*/

        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf('.'));
        //1.把文件上传oss
        String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(),aliyunOSSProperties.getBucketName(), file.getBytes(), ext);
        //2.响应数据
        return Result.success(url);
    }
}
